<?php

class Cart extends CComponent {

    public $data = array();
    public $over_quantity = false;
    public $total = -1;

    /**
     *
     * @return type 
     */
    public static function getInstance() {
        if (!isset(Yii::app()->session['cart'])) {
            Yii::app()->session['cart'] = new Cart();
        }
        return Yii::app()->session['cart'];
    }

    /**
     *
     * @param type $product_id
     * @param type $quantity
     * @param array $arribute 
     */
    public function add($product_id, $quantity, $arribute = array(), $comment = "") {
        $product = Product::model()->findByPk($product_id);
        if ($product) {
            if (!is_numeric($quantity) || (int) $quantity <= 0) {
                $quantity = 1;
            }
            if (!is_array($arribute)) {
                $arribute = array();
            }
            $key = $product_id . "_" . implode("_", $arribute);
            if (key_exists($key, $this->data)) {
                $quantity_new = $this->data[$key]['quantity'] + $quantity;
                $this->data[$key]['quantity'] = $this->data[$key]['quantity'] + $quantity;
            } else {
                $quantity_new = $quantity;
                $quantity = $product->quantity >= $quantity ? $quantity : $product->quantity;
                $this->data[$key] = array(
                    'quantity' => $quantity,
                    'arributes' => $arribute,
                    'product_id' => $product_id,
                    'comment' => $comment,
                    'over_quantity' => false
                );
            }
            if ($product->quantity >= $quantity_new) {
                Yii::app()->session['cart'] = $this;
                return 1;
            } else {
                return 0;
            }
        }
        return -1;
    }

    /**
     * 
     */
    public function update($product_id, $quantity, $arribute = array()) {
        $product = Product::model()->findByPk($product_id);
        if ($product) {
            if (!is_numeric($quantity) || (int) $quantity <= 0) {
                $quantity = 1;
            }
            if (!is_array($arribute)) {
                $arribute = array();
            }
            $key = $product_id . "_" . implode("_", $arribute);
            if (key_exists($key, $this->data)) {
                $this->data[$key]['quantity'] = $product->quantity >= $quantity ? $quantity : $product->quantity;
                if ($product->quantity >= $quantity) {
                    Yii::app()->session['cart'] = $this;
                    return 1;
                }
                return 0;
            }
            return -1;
        }
        return -1;
    }

    /**
     * 
     */
    public function delete($product_id, $quantity, $arribute = array()) {
        if (!is_array($arribute)) {
            $arribute = array();
        }
        $key = $product_id . "_" . implode("_", $arribute);
        if (key_exists($key, $this->data)) {
            unset($this->data[$key]);
            Yii::app()->session['cart'] = $this;
            return true;
        }
        return false;
    }

    /**
     * 
     */
    public function listProduct() {
        $return = array();
        $check_over = false;
        $total = 0;
        foreach ($this->data as $item) {
            $product = Product::model()->findByPk($item['product_id']);
            if ($product != null) {
                $attributes = array();
                foreach ($item['arributes'] as $value) {
                    $attribute = PropertyValue::model()->findByPk($value);
                    if ($attribute != null)
                        $attributes[] = $attribute;
                }
                $over_quantity = $product->quantity < $item['quantity'] ? true : false;
                $return[] = array(
                    'product' => $product,
                    'quantity' => $item['quantity'],
                    'attributes' => $attributes,
                    'simpleAttributes' => $item['arributes'],
                    'over_quantity' => $over_quantity
                );
                if ($over_quantity)
                    $check_over = true;
                $total += ($product->getPrice() * $item['quantity']);
            }
        }
        $this->total = $total;
        $this->over_quantity = $check_over;
        return $return;
    }

    /**
     * 
     */
    public function destroy() {
        unset(Yii::app()->session['cart']);
    }

    /**
     * 
     */
    public function countProduct() {
        return count($this->data);
    }
    
    /**
     * 
     */
    public function getTotal(){
        if($this->total < 0)
                $this->listProduct();
        return $this->total;
    }

}